package com.leapp.sk.viewdelegate.adapter.personal

import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.example.baselibrary.db.dao.AccountDao
import com.example.baselibrary.db.table.AccountTable
import com.example.mvp.bean.ClassMemberInfoEntity
import com.example.mvp.delegate.BaseAdapterDelegate
import com.example.mvp.view.GlideCircleTransform
import com.example.mvp.view.LKCircleImageView
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.bean.ClassDetialDataEntity
import com.leapp.sk.ui.activity.classs.HeadmasterDetialActivity
import com.example.util.finallist.THEACHER_DATA

/**
 * 通讯录适配器视图托管类
 * @author  chaohaipeng  on 2018/8/21.
 */
class ContanctAdapterDelegate :BaseAdapterDelegate(){
    /**
     * 老师头像加载操作
     */
    private val mHeadTRequestOptions: RequestOptions = RequestOptions()
    override fun getOptionsMenuId(): Int {
        return 0
    }

    override fun initWidget() {
    }

    override fun onDestroy() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.adapter_contanct
    }


    fun setClassName(data:ClassDetialDataEntity){
        val tv_ac_class_name = getView<TextView>(R.id.tv_ac_class_name)
        tv_ac_class_name?.text=data.className
    }
    /**
     * 初始化老师数据
     */
    fun initTechere(context: Context, data: List<ClassMemberInfoEntity>?){
        if (data==null){
            return
        }

        val ll_acd_thecher_head=  getView<LinearLayout>(R.id.ll_ac_thecher_info)
        val hv_acd=  getView<HorizontalScrollView>(R.id.hv_ac_contanct_teacher)

        for (i in data.indices){
            val get = data.get(i)
            val accountTable = AccountTable()
            accountTable.avatar=get?.avatar
            accountTable.nick=get?.nickName
            accountTable.account=get?.id
            AccountDao.getInstance().createOrUpdate(accountTable)
            val tabView = View.inflate(context, R.layout.adapter_class_detial_techer, null)
            val ll_acdt_rootview = tabView.findViewById<LinearLayout>(R.id.ll_acdt_rootview)
            val ima_acdt_avatar = tabView.findViewById<LKCircleImageView>(R.id.ima_acdt_avatar)
            val tv_acdt_headMaster = tabView.findViewById<TextView>(R.id.tv_acdt_headMaster)

            val tv_acdt_nick =tabView.findViewById<TextView>(R.id.tv_acdt_nick)
            val layoutParams: LinearLayout.LayoutParams = ll_acdt_rootview?.layoutParams as LinearLayout.LayoutParams
            layoutParams.width= LKScreenUtil.getScreenWidth()/5- LKScreenUtil.dp2px(10f)
            ll_acdt_rootview?.layoutParams=layoutParams
            setHead(context, get?.avatar, ima_acdt_avatar)
          tv_acdt_nick.text=get.nickName
            if(get.personnelType==1){
                tv_acdt_headMaster.visibility= View.VISIBLE
            }else{
                tv_acdt_headMaster.visibility= View.GONE
            }

            ll_acd_thecher_head.addView(tabView)
            ll_acdt_rootview.setOnClickListener(View.OnClickListener {
                val intent= Intent(context, HeadmasterDetialActivity::class.java)
                intent.putExtra(THEACHER_DATA,get)
                context.startActivity(intent)
            })

        }
        SKApplication.getInstance()?.mHandler?.postDelayed(object :Runnable{
            override fun run() {
                //滚动到左边
                hv_acd.smoothScrollTo(0,0)
            }

        },10)

    }
    /**
     * 初始化适配器
     */
    fun initClassMember(context: Context, data: List<ClassMemberInfoEntity>?, total:Int?){
        if (data==null){
            return
        }
        val format = String.format(context.resources.getString(R.string.string_class_member_num),total)
        val tv_acd_class_member_num=  getView<TextView>(R.id.tv_ac_class_member_num)
        val ll_acd_member=  getView<LinearLayout>(R.id.ll_ac_contanct_member)
        tv_acd_class_member_num.text=format
        for (i in data.indices){
            val get = data.get(i)
            val accountTable = AccountTable()
            accountTable.avatar=get?.avatar
            accountTable.nick=get?.nickName
            accountTable.account=get?.id
            AccountDao.getInstance().createOrUpdate(accountTable)
            val tabView = View.inflate(context, R.layout.item_class_detial_member, null)
            val ll_icdm_rootview = tabView.findViewById<LinearLayout>(R.id.ll_icdm_rootview)
            val ima_icdm_avatar = tabView.findViewById<LKCircleImageView>(R.id.ima_icdm_avatar)
            val tv_icdm_nick =tabView.findViewById<TextView>(R.id.tv_icdm_nick)
            val layoutParams:LinearLayout.LayoutParams = ll_icdm_rootview?.layoutParams as LinearLayout.LayoutParams
            layoutParams.width=LKScreenUtil.getScreenWidth()/5
            ll_icdm_rootview?.layoutParams=layoutParams
            setHead(context, get?.avatar, ima_icdm_avatar)
            tv_icdm_nick.text=get.nickName
            ll_acd_member.addView(tabView)
            ll_icdm_rootview.setOnClickListener(View.OnClickListener {
                val intent= Intent(context, HeadmasterDetialActivity::class.java)
                intent.putExtra(THEACHER_DATA,get)
                context.startActivity(intent)
            })
        }

    }
    /**
     * 设置头像和昵称
     */
    private fun setHead(context: Context, headUrl: String?, headImage: LKCircleImageView) {
        val mRequestManager = Glide.with(context)
        mHeadTRequestOptions?.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
        mHeadTRequestOptions?.skipMemoryCache(true)
        mHeadTRequestOptions?.error(R.mipmap.test_class_member)
        mHeadTRequestOptions?.transforms(GlideCircleTransform(context))
        mRequestManager?.load(headUrl)
                ?.thumbnail(0.2f)
                ?.apply(mHeadTRequestOptions)
                ?.into(headImage)
    }
}